package com.uyayo.core.interceptor;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.uyayo.framework.common.annotation.MethodName;
import com.uyayo.framework.common.util.CodeUtil;
import com.uyayo.framework.common.util.thread.Pool;
import com.uyayo.framework.common.util.thread.Task;
import com.uyayo.framework.common.util.thread.ThreadPool;
import com.uyayo.system.authority.entity.SysUser;
import com.uyayo.system.monitor.entity.SysEvent;
import com.uyayo.system.monitor.service.EventsService;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;


/**
 * 系统事件拦截器
 * 
 * @author 胡逢君
 * 
 */
public class SystemEventInterceptor implements HandlerInterceptor {

	private static final Logger logger = Logger.getLogger(SystemEventInterceptor.class);

	@Autowired
	private EventsService eventsService;

	@Override
	public void afterCompletion( HttpServletRequest request,
			HttpServletResponse response, Object obj, Exception exception)
			 {
		
		/*Subject subject = SecurityUtils.getSubject();
		final SysUser sysUser = (SysUser) subject.getPrincipal();
		final String requestURI = request.getRequestURI();

		final Date eventStartTime  =  (Date)request.getAttribute("eventStartTime");
		
		final HandlerMethod method = (HandlerMethod)obj;

		final String id = CodeUtil.getTimeSequenceAsRandom();
		
		Pool pool = ThreadPool.getInstance();
		Task etask = new Task(id, "保存系统事件中") {
			@Override
			synchronized public  void execute() {
				
				try {
					//处理HandlerMethod Annotation，实现方法级权限控制
					MethodName methodName = method.getMethodAnnotation(MethodName.class);

					logger.debug("开始退出Request事件监听器->访问路径为[" + requestURI + "]");

					SysEvent events = new SysEvent();
					events.setId(id);
					events.setCode(id);
					events.setRequestUrl(requestURI);
					events.setStartTime(eventStartTime);
					events.setEndTime(new Date());
					events.setUserId(sysUser.getId());
					String userLoginName;
					String description;
					
					userLoginName = sysUser.getLoginname();
					if (methodName!=null && methodName.validate()==true) {
						if(methodName.type()==1){
							description =  "【" + userLoginName + "】打开了菜单【" + methodName.value() + "】";
						}else if(methodName.type()==2){
							description =  "【" + userLoginName + "】使用了功能【" + methodName.value() + "】";
						}else{
							return;
						}
						events.setDescription(description);
						eventsService.insertSelective(events);
					}
				
				} catch (Exception e) {
					logger.error("事件监听器异常", e);
				}
			}
		};
		pool.execute(etask);*/
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response,
			Object obj, ModelAndView modelAndView)  {
		logger.debug("开始进入PostRequest事件监听器->访问路径为[" + request.getRequestURI() + "]");
	}

	@Override
	public boolean preHandle(final HttpServletRequest request, HttpServletResponse response,
			final Object obj)  {

		logger.debug("开始进入Request事件监听器->访问路径为[" + request.getRequestURI() + "]");
		request.setAttribute("eventStartTime", new Date());
		return true;
	}

}
